import abc
import os
from library_test.base_test import BaseLibraryTest


class TestProcessFormat(BaseLibraryTest, metaclass=abc.ABCMeta):
    TESTCASE_DIR = os.path.join(os.path.dirname(__file__), "testcase")

    @classmethod
    def get_module_path(cls):
        return "ascend_deployer.library.generate_check_result_json"

    @classmethod
    def setUpClass(cls) -> None:
        super().setUpClass()

    def setUp(self) -> None:
        from ascend_deployer.library.generate_check_result_json import OutputCheck
        self.output_check = OutputCheck()


class TestProcess(TestProcessFormat):

    @classmethod
    def get_testcase_path(cls):
        return os.path.join(cls.TESTCASE_DIR, "generate_check_result_json.yml")

    def test_format_result_to_json(self):
        self.output_check.check_results = [
            {
                "192.168.1.1": [
                    {
                        "check_item": "check_card",
                        "check_status": "success",
                        "desc_en": "Check: 1. Whether the npu card is no longer supported in the life cycle,2. Whether the npu card is in the OS list supported by this card.",
                        "desc_zh": "检查：1、npu卡是否已不在生命周期支持，2、npu卡是否在此卡支持的os列表。",
                        "error_msg": "",
                        "help_url": "https://www.hiascend.com/document/detail/zh/mindx-dl/600/ascenddeployer/ascenddeployer/deployer_0002.html",
                        "tip_en": "",
                        "tip_zh": ""
                    }
                ]
            },
            {
                "localhost": {
                    "192.168.1.2": [
                        {
                            "check_item": "check_root",
                            "check_status": "failed",
                            "desc_en": "Check whether the executing user is root.",
                            "desc_zh": "检查执行用户是否是root。",
                            "error_msg": "[ASCEND][ERROR] The installation command could only be executed by root user.",
                            "help_url": "",
                            "tip_en": "",
                            "tip_zh": ""
                        }
                    ]
                }
            }
        ]
        format_result = {
            "CheckList": [
                {
                    "check_item": "check_card",
                    "desc_en": "Check: 1. Whether the npu card is no longer supported in the life cycle,2. Whether the npu card is in the OS list supported by this card.",
                    "desc_zh": "检查：1、npu卡是否已不在生命周期支持，2、npu卡是否在此卡支持的os列表。",
                    "tip_en": "",
                    "tip_zh": "",
                    "help_url": "https://www.hiascend.com/document/detail/zh/mindx-dl/600/ascenddeployer/ascenddeployer/deployer_0002.html"
                },
                {
                    "check_item": "check_root",
                    "desc_en": "Check whether the executing user is root.",
                    "desc_zh": "检查执行用户是否是root。",
                    "tip_en": "",
                    "tip_zh": "",
                    "help_url": ""
                }
            ],
            "HostCheckResList": {
                "192.168.1.1": {
                    "check_res_list": [
                        {
                            "check_item": "check_card",
                            "status": "success"
                        }
                    ]
                },
                "localhost": {
                    "check_res_list": [
                        {
                            "check_item": "check_root",
                            "status": "failed",
                            "error_msg": "[ASCEND][ERROR] The installation command could only be executed by root user."
                        }
                    ]
                }
            }
        }
        self.assertEqual(format_result, self.output_check.format_result_to_json())
